package com.health.article.service.impl;

import com.health.article.domain.dto.ShmArticleDTO;
import com.health.article.domain.vo.ShmArticleVO;
import com.health.article.domain.vo.ShmSubjectVO;
import com.health.article.mapper.ArticleMapper;
import com.health.article.service.ArticleService;
import com.health.common.security.utils.SecurityUtils;
import com.health.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    /**
     * 查询所有文章
     * @return
     */
    @Override
    public List<ShmArticleVO> selectArticleAll() {
        List<ShmArticleVO> shmArticleVOS = articleMapper.selectArticleAll();
        return shmArticleVOS;
    }

    /**
     * 医生/患者发布创作/保存草稿
     * @param shmArticleDTO
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertArticle(ShmArticleDTO shmArticleDTO) {

        Long userid = SecurityUtils.getLoginUser().getUserid();

        if (userid == null){
            throw new RuntimeException("你还未登录，请在登录后进行创作");
        }
        shmArticleDTO.setCreatorId(userid);

        if (shmArticleDTO.getButtonType() == 1){
            shmArticleDTO.setArticleStatus(2L);
        }else {
            shmArticleDTO.setArticleStatus(0L);
        }

        articleMapper.insertArticle(shmArticleDTO);

        articleMapper.insertArticleSubject(shmArticleDTO.getId(),shmArticleDTO.getSubjectIds());

    }

    @Override
    public List<ShmArticleVO> selectMyDraftAndArticle() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userid = loginUser.getUserid();

        List<ShmArticleVO> shmArticleVOS = articleMapper.selectMyDraftAndArticle(userid);

        if (shmArticleVOS.size() == 0){
            throw new RuntimeException("暂无草稿");
        }

        return shmArticleVOS;
    }
;
    @Override
    public List<ShmSubjectVO> selectSubjectALl() {
        List<ShmSubjectVO> shmSubjectVOS = articleMapper.selectSubjectAll();
        return shmSubjectVOS;
    }

    /**
     * 查询我的文章
     * @param type
     * @return
     */
    @Override
    public List<ShmArticleVO> selectMyArticle(Long type) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userid = loginUser.getUserid();

        List<ShmArticleVO> shmArticleVOS = articleMapper.selectMyArticle(userid, type);
        if (shmArticleVOS.size() == 0){
            throw new RuntimeException("暂无发布文章");
        }

        return shmArticleVOS;
    }

    /**
     * 查询文章详情
     * @param id
     * @return
     */
    @Override
    public ShmArticleVO selectArticleInfo(Long id) {
        if (id == null){
            throw new RuntimeException("该文章已不存在");
        }
        ShmArticleVO shmArticleVO = articleMapper.selectArticleInfo(id);
        return shmArticleVO;
    }

    /**
     * 删除我的草稿
     * @param id
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteMyDraft(Long id) {
        if (id == null){
            throw new RuntimeException("该文章已不存在");
        }
        articleMapper.deleteArticleById(id);
        articleMapper.deleteArticleSubjectById(id);
    }

    /**
     * 编辑我的草稿
     * @param shmArticleDTO
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void editMyDraft(ShmArticleDTO shmArticleDTO) {

        if (shmArticleDTO.getButtonType() == 1){
            shmArticleDTO.setArticleStatus(2L);
        }else {
            shmArticleDTO.setArticleStatus(0L);
        }

        if (shmArticleDTO.getArticleStatus() == 2){
            throw new RuntimeException("文章正在正在审核中，无法修改");
        }
        articleMapper.updateMyDraft(shmArticleDTO);

        Long[] shmArticleSubjects = articleMapper.selectArticleSubjectById(shmArticleDTO.getId());
        boolean b = arraysContainSameUniqueElements(shmArticleSubjects, shmArticleDTO.getSubjectIds());
        if (!b){
            articleMapper.deleteArticleSubjectById(shmArticleDTO.getId());
            articleMapper.insertArticleSubject(shmArticleDTO.getId(),shmArticleDTO.getSubjectIds());
        }

    }


    public static boolean arraysContainSameUniqueElements(Long[] array1, Long[] array2) {
        if (array1 == null && array2 == null) {
            return true;
        }
        if (array1 == null || array2 == null || array1.length != array2.length) {
            return false;
        }
        Set<Long> set1 = new HashSet<>(Arrays.asList(array1));
        Set<Long> set2 = new HashSet<>(Arrays.asList(array2));
        return set1.equals(set2);
    }

}
